package com.ryskoo.controller.dinnertable;

import com.ryskoo.catering.dinnertable.domain.DinnerTablePO;
import com.ryskoo.catering.dinnertable.service.IDinnerTableService;
import com.ryskoo.catering.dinnertabletype.domain.DinnerTableTypePO;
import com.ryskoo.catering.dinnertabletype.service.IDinnerTableTypeService;
import com.ryskoo.customer.vendor.domain.VendorPO;
import com.ryskoo.customer.vendor.service.IVendorService;
import com.ryskoo.framework.core.bean.QueryConditionEnum;
import com.ryskoo.framework.core.bean.QueryMessageDTO;
import com.ryskoo.framework.core.bean.ReturnMessageDTO;
import com.ryskoo.framework.core.controller.BasicHandler;
import com.ryskoo.framework.core.utils.LoginUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/vendor/dinnertable")
public class DinnerTableController extends BasicHandler<DinnerTablePO> {
    private static final String TEMPLATE_PATH = "system/dinnertable";

    private static final String MODULE_NAME = "DinnerTable";

    /**
     * 构造方法
     */
    public DinnerTableController() {
        super(TEMPLATE_PATH,MODULE_NAME);
    }

    private IDinnerTableService dinnerTableService;

    @Autowired
    private IDinnerTableTypeService dinnerTableTypeService;

    @Autowired
    private IVendorService vendorService;
    
    @Autowired
    public void setService(IDinnerTableService dinnerTableService) {
        this.dinnerTableService = dinnerTableService;
        super.setService(dinnerTableService);
    }

    /**
     * 构建查询条件
     * @param qm
     */
    @Override
    protected void buildQueryMessage(QueryMessageDTO qm) {
        qm.addCondition("vendorUuid", QueryConditionEnum.EQ.name(), LoginUtil.getLoginVendorUuid());
    }

    @Override
    protected void buildListPageData(Model model, HttpServletRequest request) {
        String vendorUuid = LoginUtil.getLoginVendorUuid();
        List<DinnerTableTypePO> typeList = dinnerTableTypeService.getListByVendorId(vendorUuid);
        model.addAttribute("typeList",typeList);
        super.buildListPageData(model, request);
    }

    /**
     * 重写桌台新增的方法，将商户名称保存
     * @param po
     *            新增的对象 {@link DinnerTablePO}
     * @return
     */
    @Override
    public ReturnMessageDTO add(@RequestBody DinnerTablePO po) {
        VendorPO vendor = LoginUtil.getLoginVendorModel();
        po.setVendorUuid(vendor.getUuid());
        po.setVendorName(vendor.getVendorName());
        return super.add(po);
    }

    /**
     * 重新桌台编辑的方法，将商户名称保存
     * @param po
     *            编辑的对象 {@link DinnerTablePO}
     * @return
     */
    @Override
    public ReturnMessageDTO edit(@RequestBody DinnerTablePO po) {
        return super.edit(po);
    }
}
